package com.futuristik.objects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.futuristik.arkitek.Character;

public class FloorObject extends ArkitekObject {
	
	World world;
	Mesh floorTextureMesh;
	Texture floorTexture;
	float mu;
	
	public final static float height = 1/3f;

	public FloorObject(float x, float y, float width, float angle) {
		super(x, y, angle);
		
		this.width = width;
	}
	
	//UTILITY
	
	 protected float[] verticesForUnscaledTexture(Texture texture, float[] vertices) {

	      float[] result = new float[5 * vertices.length / 2];

	      float u = ((float) Gdx.graphics.getWidth()) / texture.getWidth()/ 24;
	      float v = ((float) Gdx.graphics.getHeight()) / texture.getHeight()/ 16;

	      for (int i = 0; i < vertices.length / 2; i++) {

	         result[5 * i] = vertices[2 * i];
	         result[5 * i + 1] = vertices[2 * i + 1];
	         result[5 * i + 2] = 0;
	         result[5 * i + 3] = u * vertices[2 * i] ;
	         result[5 * i + 4] = -v * vertices[2 * i + 1] ;

	      }
	      return result;

	   }
	
	public void create(World w){
		world = w;
		
		BodyDef def = new BodyDef();
		def.position.set(new Vector2(x, y));
		objectBody = world.createBody(def);
		
		PolygonShape poly = new PolygonShape();
			poly.setAsBox(width, height, new Vector2(), angle);
			objectBody.createFixture(poly, 1);
		FixtureDef fDef = new FixtureDef();
			fDef.friction = mu;
			fDef.shape = poly;
		objectBody.createFixture(fDef);
		poly.dispose();

	}
	
	public String toString(){
		return this.getClass().getSimpleName()+" "+x+" "+y+" "+width+" "+angle;
	}
	
	public void render(SpriteBatch batch){
		floorTexture.bind();
		floorTextureMesh.render(GL10.GL_TRIANGLE_STRIP, 0, 4);
	}

	public void playerContact(Character player, Contact cont){
		cont.setFriction(mu);
	}
}
